﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using BIZ;
using DAL;
namespace SinhNhat.admin.slider
{
    public partial class CentralBar : System.Web.UI.Page
    {
        private static bool flag_edit = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack && (base.Request.QueryString["id"] != null))
            {
                string id = base.Request.QueryString["id"];
                IQueryable<CENTRALBAR> source = from cen in WebUtils.db.CENTRALBARs
                                                where cen.IdCentralBar.ToString() == id
                                                select cen;
                this.name.Value = source.FirstOrDefault<CENTRALBAR>().Name;
                this.link.Value = source.FirstOrDefault<CENTRALBAR>().Link;
                this.CentralBar_image.Attributes["src"] = source.FirstOrDefault<CENTRALBAR>().Image;
                this.ImageHidden.Value = source.FirstOrDefault<CENTRALBAR>().Image;
                flag_edit = true;
            }
            else
            {
                if (base.IsPostBack)
                {
                    CENTRALBAR entity = new CENTRALBAR
                    {
                        Name = this.name.Value,
                        Link = this.link.Value,
                        Image = this.ImageHidden.Value
                    };
                    this.CentralBar_image.Attributes["src"] = this.ImageHidden.Value;
                    if (!flag_edit)
                    {
                        entity.Status = true;
                        WebUtils.db.CENTRALBARs.InsertOnSubmit(entity);
                    }
                    else
                    {
                        CENTRALBAR centralbar2 = WebUtils.db.CENTRALBARs.Single<CENTRALBAR>(p => p.IdCentralBar.ToString() == this.Request.QueryString["id"]);
                        centralbar2.Name = entity.Name;
                        centralbar2.Link = entity.Link;
                        centralbar2.Image = entity.Image;
                    }
                    try
                    {
                        WebUtils.db.SubmitChanges();
                        if (!flag_edit)
                        {
                            base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        }
                        else
                        {
                            this.ShowMessage("Success. Let Continue", "");
                        }
                        return;
                    }
                    catch (Exception)
                    {
                        this.ShowMessage("Fail. Please Try Again", "");
                    }
                }
                flag_edit = false;
            }
        }


        private void ShowMessage(string mTextMsg, string mControlFocus)
        {
            StringBuilder builder = new StringBuilder("");
            string absoluteUri = base.Request.Url.AbsoluteUri;
            builder.Append("<script language=\"JavaScript\">");
            if (mTextMsg != "")
            {
                builder.Append("  alert('" + mTextMsg + "');");
            }
            if (mControlFocus != "")
            {
                builder.Append("document.forms[0].item('" + mControlFocus + "').focus();");
            }
            builder.Append("</script>");
            if (!base.IsStartupScriptRegistered("setFocus"))
            {
                this.RegisterStartupScript("setFocus", builder.ToString());
            }
        }

 

 

    }
}